﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class FireBall : MonoBehaviour
{   
    public float speed = 100;
    public float duration = 1;
    private float deathTime;
    private PhotonView photView;
    private int team;
    private int playerId;
    void Awake()
    {   
        photView = this.GetComponent<PhotonView>();
        deathTime = Time.time + duration;
        if (photView.isMine)
        {
            team = GameManager.instance.photonNetworkManager.myTeam;
            playerId = GameManager.instance.localPlayerID;
        }
    }
    void Update()
    {
        if (Time.time>deathTime)
        {
            Destroy();
        }
        transform.position = transform.position + 0.01F * speed * transform.forward;
    }
    void OnTriggerEnter(Collider collider)
    {   
        if (photView.isMine)
        {
            if (collider.transform.gameObject.layer == 0)
            {   
                LifeController lc = collider.transform.root.GetComponent<LifeController>();
                if (lc == null)
                {
                    Destroy();
                }else
                {
                    if ((lc.teamAble.team != team) && lc.teamAble.team != 3)
                    {
                        lc.TakeDamage(2,playerId);
                        Destroy();
                    }
                }
            }
        }
       
    }

    void Destroy()
    {
        if (photView.isMine)
        {
            PhotonNetwork.Destroy(this.transform.gameObject);
        }
    }
}
